import chatList from "../mock/chatList.ts";
import friendList from "../mock/friendList.ts";
import {
    ChatItem,
    ChatListData,
    ChatMessageData,
    FriendDTO,
    FriendsListData,
    Group,
    GroupUser,
    Result,
    UserInfo
} from "common";
import chatData from "../mock/chatData.ts";
import axios from "axios";
import ChatData from "../mock/chatData.ts";

import {Body} from "@tauri-apps/api/http";
import {getCurrentInstance} from "vue";
import {http} from "@tauri-apps/api";
const BASE_URL = import.meta.env.VITE_BACKEND_URL
console.log(BASE_URL)
let cookie = ``
export const setCookie = (str:string)=>{
    cookie=str;
}
const mockData = (data:any)=>{
    return new Promise<any>((resolve)=>{
        setTimeout(()=>{
            resolve(data)
        },200)
    })
}

export const getChatList = ()=>{
    return http.fetch<Result<ChatListData>>(`${BASE_URL}/chat-session`,{method:"GET",headers:{Cookie:cookie}})
}

export const getGroups = ()=>{
    return http.fetch<Result<Group[]>>(`${BASE_URL}/groups`,{method:"GET",headers:{Cookie:cookie}})
}


export const getFriendList = () => {
    return http.fetch<Result<FriendDTO[]>>(`${BASE_URL}/friends`,{method:"GET",headers:{Cookie:cookie}})
}

export const loadChatHistory = (userId:number,pageIndex:number,pageSize:number)=>{
    return http.fetch<Result<ChatMessageData[]>>(`${BASE_URL}/user/${userId}/history`,{method:"GET",query:{pageIndex:pageIndex.toString(),pageSize:pageSize.toString()},headers:{Cookie:cookie}})
}

export const loadGroupChatHistory = (groupId:number,pageIndex:number,pageSize:number)=>{
    return http.fetch<Result<ChatMessageData[]>>(`${BASE_URL}/group/${groupId}/history`,{method:"GET",query:{pageIndex:pageIndex.toString(),pageSize:pageSize.toString()},headers:{Cookie:cookie}})
}

export const login = (username:string,password:string)=>{
    return http.fetch<Result<any>>(`${BASE_URL}/login`,{method:"POST",body:Body.json({username,password})})
}

export const register = (username:string,password:string,email:string)=>{
    return http.fetch<Result<any>>(`${BASE_URL}/register`,{method:"POST",body:Body.json({username,password,email})})
}

export const getGroupUsers = (id:number)=>{
    return http.fetch<Result<GroupUser[]>>(`${BASE_URL}/group/${id}/users`)
}

export const updateUserProfile = (name: string, avatar: string) => {
    return http.fetch<Result<UserInfo>>(`${BASE_URL}/user/profile`, {
        method: "PUT",
        body: Body.json({ name, avatar }),
        headers: { Cookie: cookie }
    })
}